package cn.weige.hotel.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {


    /**
     * 定义交换机
     * @return
     */
    @Bean
    public TopicExchange topicExchange(){
        /*
            参数1：交换机名称
            参数2： 是否持久化 true:持久化
            参数3：没有队列与交换机绑定时是否删除，false表示不删除
         */
       return   new TopicExchange("hotel.topic",true,false);
    }

    /**
     * 配置插入|更新信息的队列
     * @return
     */
    @Bean
    public Queue insertQueue(){
       return  new Queue("hotel.insert.queue",true);
    }
    /**
     * 配置删除信息的队列
     * @return
     */
    @Bean
    public Queue deleteQueue(){
       return  new Queue("hotel.delete.queue",true);
    }

    /**
     * 绑定插入队列到主题交换机
     * @return
     */
    @Bean
    public Binding bindingInsertQueue(){
        return BindingBuilder.bind(insertQueue()).to(topicExchange()).with("hotel.insert");
    }

    /**
     * 绑定删除队列到主题交换机
     * @return
     */
    @Bean
    public Binding bindingDeleteQueue(){
        return BindingBuilder.bind(deleteQueue()).to(topicExchange()).with("hotel.delete");
    }

    @Bean
    public MessageConverter jsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }



}
